package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.ChatActivity;
import com.xgkj.diyiketang.activity.HisDetailActivity;
import com.xgkj.diyiketang.activity.NewFriendActivity;
import com.xgkj.diyiketang.activity.RecommendAlumnusActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/8/20.
 */
public class FriendAdapter extends BaseAdapter {


    private Context mContext;

    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public FriendAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size() + 2;
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position - 2);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (position == 0) {
            if (convertView != null && convertView instanceof LinearLayout) {

            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.friend_item_newfriend, null);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, NewFriendActivity.class);
                    mContext.startActivity(intent);
                }
            });
            return convertView;
        }
        if (position == 1) {
            if (convertView != null && convertView instanceof LinearLayout) {

            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.friend_item_recommfriend, null);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, RecommendAlumnusActivity.class);
                    mContext.startActivity(intent);
                }
            });
            return convertView;
        }
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fans_item, parent, false);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.personSignatureText = (TextView) convertView.findViewById(R.id.person_signature);
            childHolder.sendMessageBtn = (ImageButton) convertView.findViewById(R.id.send_message);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }


        final EatShowUserInfo fansInfo = mList.get(position - 2);
        if (fansInfo != null) {
            String headImgUrl = fansInfo.getUserLogo();
            if (headImgUrl != null && headImgUrl.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl, size));
                if (cacheBitmap != null) {
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headImgUrl, size);
                }
            } else {
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }
            if (fansInfo.getNickName() != null) {
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(fansInfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            if (fansInfo.getUserSign() != null) {
                try {
                    childHolder.personSignatureText.setText(URLDecoder.decode(fansInfo.getUserSign(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }


            childHolder.sendMessageBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 发消息界面
                    String Uid = fansInfo.getUserId().toLowerCase();
                    String opid = fansInfo.getOpenId();
                    if (opid != null && opid.length() > 1) {
                        Uid = opid.toLowerCase();
                    }

                    EatShowUserInfo myInfo = EatShowApp.getInstance().getmUserInfo();
                    String myId = TextUtils.isEmpty(myInfo.getOpenId()) ? myInfo.getUserId() : myInfo.getOpenId();
                    if (myId.equals(Uid)) {
                        ToastUtil.showMessage(mContext, R.string.Cant_chat_with_yourself);
                        return;
                    }

                    Intent intent = new Intent(mContext, ChatActivity.class);
                    intent.putExtra("userId", Uid);
                    intent.putExtra("userNick", fansInfo.getNickName());
                    intent.putExtra("myLogo", EatShowApp.getInstance().getmUserInfo().getUserLogo());
                    intent.putExtra("fromLogo", fansInfo.getUserLogo());
                    mContext.startActivity(intent);
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                        Intent intent = new Intent(mContext, com.xgkj.diyiketang.activity.UserDetailActivity.class);
//                        intent.putExtra("userId", fansInfo.getUserId());
//                        mContext.startActivity(intent);
                    String uid = fansInfo.getUserId();

                    Intent intent = new Intent(mContext, HisDetailActivity.class);
                    intent.putExtra("USERID", uid);

                    mContext.startActivity(intent);
                }
            });
        }

        return convertView;
    }

    class ChildHolder {
        ImageView headImg;
        TextView nickNameText;
        TextView personSignatureText;
        ImageButton sendMessageBtn;
    }
}

